
/************************************************************************************
*	Copyright Paxs Electronics 2012													*
*   All Rights Reserved																*
*   The Copyright symbol does not also indicate public availability or publication.	*
* 																					*
* 								PAXS ELECTRONICS									*
* 																					*
* - Driver:   			USART Commonications										*
* 																					*
* - Compiler:           AVR Studio 5.0												*
* - Supported devices:  All AVR MEGA devices.										*
* - AppNote:																		*
*																					*
* 	Created by: 		Juan Francisco Gomez Ruiz									*
*   Date Created: 		24 - Apr - 2012												*
*   Contac				pacoduz@gmail.com											*
* 																					*
* 	Description: 		Provide functions that handle USART TX/RX communications	*
*						MPCM function available										*
*   Device supported and tested:													*
*						- ATmega32													*
* 																				2012*
*************************************************************************************

*************************************************************************************
* 	Revision History:
*
*   Date          CP#           Author
*   MM-DD-YY      XXXXX:1       Initials   Description of change
*   -----------   -----------   --------   ------------------------------------
*    04-24-12      	1.0             JFGR	Initial version. 

*************************************************************************************
* 	File/
* 
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
*  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
*  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
*  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,  
*  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
*  DEALINGS IN THE SOFTWARE.
* 
*/

#ifndef USARTCOMMUNICATIONS_H_
#define USARTCOMMUNICATIONS_H_
	#include "inc/hw_ints.h"
	#include "inc/hw_memmap.h"
	//#include "inc/hw_sysctl.h"
	#include "inc/hw_types.h"
	#include "driverlib/uart.h"
	#include "DataIntegrity.h"
/******************************************************************************
*							GLOBAL MACRO DEFINITIONS
******************************************************************************/
	#define M_uCONTROLLER_ADDRESS	0xAA	//Master uController address
	#define FINISH_BYTE				'!'
	#define ADDRESS_ENABLE			(ON)
	
	#define UART_DATA_DIGIT			1	
	#define UART_DATA_LENGTH		(5 * UART_DATA_DIGIT)	
	#define UART_PACKAGE_LENGTH		(10 * UART_DATA_DIGIT)
	#define UART_DATA_START			2
	#define UART_HEADER1			'X'
	#define UART_HEADER_LENGTH		2		//1 Headers plus Address
	

/******************************************************************************
*   GloBAL Type Definitions
******************************************************************************/
	typedef struct						//Structure to handle State Machin, that controls:
	{											 
		unsigned char ActualState;
		unsigned char NextState;
	}USARTSM;

/******************************************************************************
*   Global Variable Definitions
******************************************************************************/
	extern unsigned char USARTTXDataBuffer[UART_DATA_LENGTH],USARTRXDataBuffer[UART_DATA_LENGTH];
	extern volatile unsigned char  UsartTXCounter,UsartRXCounter;
	
/******************************************************************************
*   Static Variable Definitions
******************************************************************************/

/******************************************************************************
*   Global and Static Function Definitions
******************************************************************************/
	void VfnUSARTTxCommunicationInit(void);	
	void VfnUSARTRxCommunicationInit(void);
	unsigned char VfnUSARTTxCommunication (void);
	unsigned char VfnUSARTRxCommunication (void);
	void VfnUSARTTxService (void);
	void VfnUSARTRxService (void);
	unsigned char VfnUSARTPackageValidation(void);

#endif /* USARTCOMMUNICATIONS_H_ */
